package com.itheima.web.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Xiao
 * @date 2021/8/30
 */
@WebServlet("/login")
public class UserServlet extends HttpServlet {
    /**
     * 模拟登录：
     *      正确用户名密码：zhangsan    123
     *      如果用户名密码正确就输出登录成功，否则输出登录失败。
     */
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uname = "zhangsan";
        String pwd = "123";
        req.setCharacterEncoding("UTF-8");
        Map<String, String[]> map = req.getParameterMap();
        ArrayList<String> list = new ArrayList<>();
        for (Map.Entry<String, String[]> entry : map.entrySet()) {
            list.add(entry.getKey());
        }
        String username = req.getParameter(list.get(0));
        String password = req.getParameter(list.get(1));
        boolean flag = username.equals(uname) && password.equals(pwd);
        if (flag) {
            System.out.println("登录成功。。。");
        } else {
            System.out.println("登录失败。。。");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
